package com.kingdee.eas.custom.hostelmgr.quickdistribution.app;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectStringPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hostelmgr.basedata.DistributionOrNotEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicFactory;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicInfo;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionCollection;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionFactory;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.util.app.DbUtil;

public class QuickDistributionFacadeControllerBean extends
		AbstractQuickDistributionFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hostelmgr.quickdistribution.app.QuickDistributionFacadeControllerBean");

	public void match(Context ctx,
			BedDistributionCollection distributionCollection)
			throws BOSException, EASBizException {
		super.match(ctx, distributionCollection);

		CoreBaseCollection personCollection = new CoreBaseCollection();
		CoreBaseCollection collection = new CoreBaseCollection();

		List<Object[]> list = new ArrayList<Object[]>();

		BedDistributionInfo distribution = null;
		for (int i = 0; i < distributionCollection.size(); i++) {
			distribution = distributionCollection.get(i);

			PersonInfo person = distribution.getPerson();
			HostelBasicInfo bed = distribution.getBed();
			HostelBasicInfo exisitbed = null;
			if (!this.checkPersonExisit(ctx, person.getId().toString())) {
				continue;
			}

			if ((exisitbed = this.checkBedExisit(ctx, bed.getId().toString())) == null) {
				continue;
			}

			BedDistributionInfo personDistribution = this
					.checkPersonIsDistribution(ctx, person.getId().toString());
			BedDistributionInfo bedDistribution = this.checkBedIsDistribution(
					ctx, bed.getId().toString());
			if (personDistribution != null) {
				personDistribution.setBed(bed);
				personDistribution.setStayDate(distribution.getStayDate());
				collection.add(personDistribution);

				String sql = "/*dialect*/UPDATE CT_HOS_BEDDISTRIBUTION SET CFTREEIDID=?,CFSTAYDATE=? WHERE FID=?";
				DbUtil.execute(ctx, sql, new Object[] {
						personDistribution.getBed().getId().toString(),
						distribution.getStayDate(),
						personDistribution.getId().toString() });

			} else if (bedDistribution != null) {

				PersonInfo personInfo = bedDistribution.getPerson();
				if (personInfo != null) {
					personInfo.setBed(null);
					personInfo.setIsDistribution(DistributionOrNotEnum.NO);
					personCollection.add(personInfo);
				}
				person.setIsDistribution(DistributionOrNotEnum.YES);
				personCollection.add(person);
				String distributionSql = "UPDATE CT_HOS_BEDDISTRIBUTION SET CFPERSONID=?,CFSTAYDATE=? WHERE CFTREEIDID=?";
				DbUtil.execute(ctx, distributionSql, new Object[] {
						person.getId().toString(), distribution.getStayDate(),
						bed.getId().toString() });
				// collection.add(distribution);
				// BedDistributionFactory.getLocalInstance(ctx).update(new
				// ObjectUuidPK(distribution.getId()), distribution);
			}
			Object[] param = new Object[] { exisitbed.getDisplayName(),
					person.getId().toString() };
			list.add(param);
		}
		String sql = "UPDATE T_HR_PERSONCONTACTMETHOD SET FADDRESS=? WHERE FPERSONID=?";

		PersonFactory.getLocalInstance(ctx).updateBatchData(personCollection);
		DbUtil.executeBatch(ctx, sql, list);
	}

	public void unMatch(Context ctx,
			BedDistributionCollection distributionCollection)
			throws BOSException, EASBizException {
		super.unMatch(ctx, distributionCollection);
		CoreBaseCollection personCollection = new CoreBaseCollection();
		CoreBaseCollection collection = new CoreBaseCollection();

		HostelBasicInfo exisitbed = null;
		List<Object[]> list = new ArrayList<Object[]>();

		BedDistributionInfo distribution = null;
		for (int i = 0; i < distributionCollection.size(); i++) {
			distribution = distributionCollection.get(i);

			PersonInfo person = distribution.getPerson();
			HostelBasicInfo bed = distribution.getBed();

			if (!this.checkPersonExisit(ctx, person.getId().toString())) {
				BedDistributionInfo bedDistributionInfo = this
						.checkPersonIsDistribution(ctx, person.getId()
								.toString());
				if (bedDistributionInfo != null) {
					bedDistributionInfo.setPerson(null);
					collection.add(bedDistributionInfo);
				}
				continue;
			}

			if ((exisitbed = this.checkBedExisit(ctx, bed.getId().toString())) == null) {
				continue;
			}

			BedDistributionInfo personDistribution = this
					.checkPersonIsDistribution(ctx, person.getId().toString());
			// BedDistributionInfo bedDistribution =
			// this.checkBedIsDistribution(ctx, bed.getId().toString());

			if (personDistribution == null) {
				continue;

			} else if (personDistribution != null
					&& !personDistribution.getBed().getId().equals(bed.getId())) {
				continue;
			}

			// PersonInfo personInfo = distribution.getPerson();
			person.setIsDistribution(DistributionOrNotEnum.NO);
			personCollection.add(person);

			distribution.setPerson(null);
			collection.add(distribution);

			Object[] param = new Object[] { "", person.getId().toString() };
			list.add(param);
		}
		BedDistributionFactory.getLocalInstance(ctx)
				.updateBatchData(collection);
		PersonFactory.getLocalInstance(ctx).updateBatchData(personCollection);

		String sql = "UPDATE T_HR_PERSONCONTACTMETHOD SET FADDRESS=? WHERE FPERSONID=?";
		DbUtil.executeBatch(ctx, sql, list);
	}

	private HostelBasicInfo checkBedExisit(Context ctx, String id)
			throws EASBizException, BOSException {
		return HostelBasicFactory.getLocalInstance(ctx).getHostelBasicInfo(
				new ObjectStringPK(id));
	}

	private boolean checkPersonExisit(Context ctx, String id)
			throws EASBizException, BOSException {
		return PersonFactory.getLocalInstance(ctx).exists(
				new ObjectStringPK(id));
	}

	private BedDistributionInfo checkPersonIsDistribution(Context ctx,
			String personId) throws BOSException {
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("person.id", personId, CompareType.EQUALS));
		view.setFilter(filter);

		BedDistributionCollection collection = BedDistributionFactory
				.getLocalInstance(ctx).getBedDistributionCollection(view);
		if (collection.size() > 0) {
			return collection.get(0);
		} else {
			return null;
		}
		// return
	}

	private BedDistributionInfo checkBedIsDistribution(Context ctx,
			String personId) throws BOSException {

		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("bed.id", personId, CompareType.EQUALS));
		view.setFilter(filter);

		BedDistributionCollection collection = BedDistributionFactory
				.getLocalInstance(ctx).getBedDistributionCollection(view);
		if (collection.size() > 0) {
			return collection.get(0);
		} else {
			return null;
		}
	}
}